<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('新增子类章节')"/>
    <th:block th:include="include :: bootstrap-fileinput-css"/>
    <th:block th:include="include :: jasny-bootstrap-css"/>
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-sonSection-add">
        <div class="form-group">
            <label class="col-sm-3 control-label">子章节名称：</label>
            <div class="col-sm-8">
                <input class="form-control" name="sonSectionName" type="text">
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label">视频：</label>
            <div class="col-sm-8">
                <!-- input用来存储上传路径到数据库 -->
                <input class="form-control" id="technician_video" name="sonSectionVideo" style="display: none"
                       type="text">
                <div class="fileinput fileinput-new input-group" data-provides="fileinput" id="inputsonSectionVideo">
                    <!-- 此div用来回显预览上传完成后的视频 -->
                    <div class="fileinput-preview fileinput-exists thumbnail" id="viderSource">
                    </div>
                    <!--相关按钮等 -->
                    <div>
                        <span class="btn btn-white btn-file">
                            <span class="fileinput-new">选择文件</span>
                            <span class="fileinput-exists">更改</span>
                            <input id="sonSectionVideo" type="file">
                        </span>
                        <a class="btn btn-white fileinput-exists" data-dismiss="fileinput" href="#">清除</a>
                    </div>
                </div>
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label">是否允许试看</label>
            <div class="col-sm-8">
                <select class="form-control m-b" name="sonSectionShow"
                        th:with="type=${@dict.getType('course_carousel')}">
                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">备注：</label>
            <div class="col-sm-8">
                <input class="form-control" name="sonSectionText" type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">父子章节关联：</label>
            <div class="col-sm-8">
                <select class="form-control m-b" name="sonFatherSection">
                    <option th:each="name:${listName}" th:text="${name.fatherSectionName}"
                            th:value="${name.fatherSectionId}"></option>
                </select>
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: bootstrap-fileinput-js"/>
<th:block th:include="include :: jasny-bootstrap-js"/>
<script th:inline="javascript">
    var prefix = ctx + "tianliangedu/sonSection"
    $("#form-sonSection-add").validate({
        focusCleanup: true
    });

    function submitHandler() {
        if ($.validate.form()) {
            $.operate.save(prefix + "/add", $('#form-sonSection-add').serialize());
        }
    }

    /*上传开始*/
    $('#inputsonSectionVideo').on('change.bs.fileinput ', function (e) {
        let imgFile = $('#sonSectionVideo')[0].files[0];
        let video = "video";
        uploadFile(imgFile, video);
    });

    //ruoyi通用的上传接口，不需要自己写后台，框架自带
    function uploadFile(imgFile, imgId) {
        var formData = new FormData();
        if (imgFile == null) {
            $.modal.alertWarning("请先选择文件路径");
            return false;
        }
        formData.append('file', imgFile);
        $.ajax({
            url: "/common/upload",
            type: 'post',
            cache: false,
            data: formData,
            processData: false,
            contentType: false,
            dataType: "json",
            async: false,
            success: function (result) {
                //给input赋值
                $('#technician_video').val(result.fileName);
                //用来回显预览视频
                let videoElement = '<video controls="controls" width="100%"><source type="video/mp4" src="' + result.fileName + '"/></video>\n';
                $("#viderSource").append(videoElement);
            }
        });
    }

    /*上传结束*/

</script>
</body>
</html>